<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 下面就是树形控件内容了 -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <tree-tools slot-scope="{data}" :tree-node="data" @addDepts="addDepts" @editDepts="editDepts" @delDepts="getDepartments" />

        </el-tree>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <add-dept ref="add-depts" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  components: { treeTools, AddDept },
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      departs: [],
      company: {},
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      console.log(result)
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '')
    },
    // 监听 tree-tools 里面添加
    addDepts(node) {
      this.node = node
      this.$refs['add-depts'].showDialog = true
    },
    // 监听 tree-tools 里面修改编辑
    editDepts(node) {
      this.node = node
      this.$refs['add-depts'].showDialog = true
      this.$refs['add-depts'].getDepartDetail(node.id)
    }
    // 点击编辑触发的父组件的方法

  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
